/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.dynamic;

import org.anderground.core.graphics.Canvas;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * 
 */
class CompoundBacillus extends Bacillus {

	private Bacillus primaryBacillus, secondaryBacillus;

	public CompoundBacillus(CompoundAmoeba compoundAmoeba,
			Amoeba oddAmoeba, Bacillus primaryBacillus,
			Bacillus secondaryBacillus) {
		this.primaryBacillus = primaryBacillus;
		this.secondaryBacillus = secondaryBacillus;
		// TODO
		assert (primaryBacillus.compoundBacillus == null);
		primaryBacillus.compoundBacillus = this;
		secondaryBacillus.compoundBacillus = this;

		this.evenAmoeba = compoundAmoeba;
		this.oddAmoeba = oddAmoeba;
		compoundAmoeba.addNeighbor(oddAmoeba, this);
		oddAmoeba.addNeighbor(compoundAmoeba, this);

		this.transfersSize = primaryBacillus.transfersSize
				+ secondaryBacillus.transfersSize;
		this.tunnelsSize = primaryBacillus.tunnelsSize
				+ secondaryBacillus.tunnelsSize;

		this.transferParts.addAll(primaryBacillus.transferParts);
		this.tunnelParts.addAll(primaryBacillus.tunnelParts);
		this.transferParts.addAll(secondaryBacillus.transferParts);
		this.tunnelParts.addAll(secondaryBacillus.tunnelParts);

	}

	public CompoundBacillus(CompoundAmoeba compoundAmoeba,
			Amoeba oddAmoeba, Bacillus bacillus) {
		this.primaryBacillus = bacillus;
		// TODO
		assert (bacillus.compoundBacillus == null);
		bacillus.compoundBacillus = this;

		this.evenAmoeba = compoundAmoeba;
		this.oddAmoeba = oddAmoeba;
		compoundAmoeba.addNeighbor(oddAmoeba, this);
		oddAmoeba.addNeighbor(compoundAmoeba, this);

		this.transfersSize = bacillus.transfersSize;
		this.tunnelsSize = bacillus.tunnelsSize;

		this.transferParts.addAll(bacillus.transferParts);
		this.tunnelParts.addAll(bacillus.tunnelParts);

	}

	@Override
	protected void draw(Canvas canvas) {
		if (((CompoundAmoeba) evenAmoeba).getCurrentDistance() > Config.mergeDistance) {
			primaryBacillus.draw(canvas);
			if (secondaryBacillus != null) {
				secondaryBacillus.draw(canvas);
			}
		} else {
			super.draw(canvas);
		}
	}

}
